<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>iter.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_iter_iter.js.html">iter.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Python style iteration utilities.
<a name="line17"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.iter&#39;);
<a name="line22"></a>goog.provide(&#39;goog.iter.Iterator&#39;);
<a name="line23"></a>goog.provide(&#39;goog.iter.StopIteration&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>// TODO(nnaze): Add more functions from Python&#39;s itertools.
<a name="line30"></a>// http://docs.python.org/library/itertools.html
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * @typedef {goog.iter.Iterator|{length:number}|{__iterator__}}
<a name="line35"></a> */
<a name="line36"></a>goog.iter.Iterable;
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>// For script engines that already support iterators.
<a name="line40"></a>if (&#39;StopIteration&#39; in goog.global) {
<a name="line41"></a>  /**
<a name="line42"></a>   * Singleton Error object that is used to terminate iterations.
<a name="line43"></a>   * @type {Error}
<a name="line44"></a>   */
<a name="line45"></a>  goog.iter.StopIteration = goog.global[&#39;StopIteration&#39;];
<a name="line46"></a>} else {
<a name="line47"></a>  /**
<a name="line48"></a>   * Singleton Error object that is used to terminate iterations.
<a name="line49"></a>   * @type {Error}
<a name="line50"></a>   * @suppress {duplicate}
<a name="line51"></a>   */
<a name="line52"></a>  goog.iter.StopIteration = Error(&#39;StopIteration&#39;);
<a name="line53"></a>}
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>/**
<a name="line58"></a> * Class/interface for iterators.  An iterator needs to implement a {@code next}
<a name="line59"></a> * method and it needs to throw a {@code goog.iter.StopIteration} when the
<a name="line60"></a> * iteration passes beyond the end.  Iterators have no {@code hasNext} method.
<a name="line61"></a> * It is recommended to always use the helper functions to iterate over the
<a name="line62"></a> * iterator or in case you are only targeting JavaScript 1.7 for in loops.
<a name="line63"></a> * @constructor
<a name="line64"></a> */
<a name="line65"></a>goog.iter.Iterator = function() {};
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * Returns the next value of the iteration.  This will throw the object
<a name="line70"></a> * {@see goog.iter#StopIteration} when the iteration passes the end.
<a name="line71"></a> * @return {*} Any object or value.
<a name="line72"></a> */
<a name="line73"></a>goog.iter.Iterator.prototype.next = function() {
<a name="line74"></a>  throw goog.iter.StopIteration;
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Returns the {@code Iterator} object itself.  This is used to implement
<a name="line80"></a> * the iterator protocol in JavaScript 1.7
<a name="line81"></a> * @param {boolean=} opt_keys  Whether to return the keys or values. Default is
<a name="line82"></a> *     to only return the values.  This is being used by the for-in loop (true)
<a name="line83"></a> *     and the for-each-in loop (false).  Even though the param gives a hint
<a name="line84"></a> *     about what the iterator will return there is no guarantee that it will
<a name="line85"></a> *     return the keys when true is passed.
<a name="line86"></a> * @return {!goog.iter.Iterator} The object itself.
<a name="line87"></a> */
<a name="line88"></a>goog.iter.Iterator.prototype.__iterator__ = function(opt_keys) {
<a name="line89"></a>  return this;
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Returns an iterator that knows how to iterate over the values in the object.
<a name="line95"></a> * @param {goog.iter.Iterable} iterable  If the object is an iterator it
<a name="line96"></a> *     will be returned as is.  If the object has a {@code __iterator__} method
<a name="line97"></a> *     that will be called to get the value iterator.  If the object is an
<a name="line98"></a> *     array-like object we create an iterator for that.
<a name="line99"></a> * @return {!goog.iter.Iterator} An iterator that knows how to iterate over the
<a name="line100"></a> *     values in {@code iterable}.
<a name="line101"></a> */
<a name="line102"></a>goog.iter.toIterator = function(iterable) {
<a name="line103"></a>  if (iterable instanceof goog.iter.Iterator) {
<a name="line104"></a>    return iterable;
<a name="line105"></a>  }
<a name="line106"></a>  if (typeof iterable.__iterator__ == &#39;function&#39;) {
<a name="line107"></a>    return iterable.__iterator__(false);
<a name="line108"></a>  }
<a name="line109"></a>  if (goog.isArrayLike(iterable)) {
<a name="line110"></a>    var i = 0;
<a name="line111"></a>    var newIter = new goog.iter.Iterator;
<a name="line112"></a>    newIter.next = function() {
<a name="line113"></a>      while (true) {
<a name="line114"></a>        if (i &gt;= iterable.length) {
<a name="line115"></a>          throw goog.iter.StopIteration;
<a name="line116"></a>        }
<a name="line117"></a>        // Don&#39;t include deleted elements.
<a name="line118"></a>        if (!(i in iterable)) {
<a name="line119"></a>          i++;
<a name="line120"></a>          continue;
<a name="line121"></a>        }
<a name="line122"></a>        return iterable[i++];
<a name="line123"></a>      }
<a name="line124"></a>    };
<a name="line125"></a>    return newIter;
<a name="line126"></a>  }
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>  // TODO(arv): Should we fall back on goog.structs.getValues()?
<a name="line130"></a>  throw Error(&#39;Not implemented&#39;);
<a name="line131"></a>};
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Calls a function for each element in the iterator with the element of the
<a name="line136"></a> * iterator passed as argument.
<a name="line137"></a> *
<a name="line138"></a> * @param {goog.iter.Iterable} iterable  The iterator to iterate
<a name="line139"></a> *     over.  If the iterable is an object {@code toIterator} will be called on
<a name="line140"></a> *     it.
<a name="line141"></a>* @param {function(this:T,?,?,?):?} f  The function to call for every
<a name="line142"></a> *     element.  This function
<a name="line143"></a> *     takes 3 arguments (the element, undefined, and the iterator) and the
<a name="line144"></a> *     return value is irrelevant.  The reason for passing undefined as the
<a name="line145"></a> *     second argument is so that the same function can be used in
<a name="line146"></a> *     {@see goog.array#forEach} as well as others.
<a name="line147"></a> * @param {T=} opt_obj  The object to be used as the value of &#39;this&#39; within
<a name="line148"></a> *     {@code f}.
<a name="line149"></a> * @template T
<a name="line150"></a> */
<a name="line151"></a>goog.iter.forEach = function(iterable, f, opt_obj) {
<a name="line152"></a>  if (goog.isArrayLike(iterable)) {
<a name="line153"></a>    /** @preserveTry */
<a name="line154"></a>    try {
<a name="line155"></a>      // NOTES: this passes the index number to the second parameter
<a name="line156"></a>      // of the callback contrary to the documentation above.
<a name="line157"></a>      goog.array.forEach(/** @type {goog.array.ArrayLike} */(iterable), f,
<a name="line158"></a>                         opt_obj);
<a name="line159"></a>    } catch (ex) {
<a name="line160"></a>      if (ex !== goog.iter.StopIteration) {
<a name="line161"></a>        throw ex;
<a name="line162"></a>      }
<a name="line163"></a>    }
<a name="line164"></a>  } else {
<a name="line165"></a>    iterable = goog.iter.toIterator(iterable);
<a name="line166"></a>    /** @preserveTry */
<a name="line167"></a>    try {
<a name="line168"></a>      while (true) {
<a name="line169"></a>        f.call(opt_obj, iterable.next(), undefined, iterable);
<a name="line170"></a>      }
<a name="line171"></a>    } catch (ex) {
<a name="line172"></a>      if (ex !== goog.iter.StopIteration) {
<a name="line173"></a>        throw ex;
<a name="line174"></a>      }
<a name="line175"></a>    }
<a name="line176"></a>  }
<a name="line177"></a>};
<a name="line178"></a>
<a name="line179"></a>
<a name="line180"></a>/**
<a name="line181"></a> * Calls a function for every element in the iterator, and if the function
<a name="line182"></a> * returns true adds the element to a new iterator.
<a name="line183"></a> *
<a name="line184"></a> * @param {goog.iter.Iterable} iterable The iterator to iterate over.
<a name="line185"></a> * @param {function(this:T,?,undefined,?):boolean} f The function to call for
<a name="line186"></a> *     every element. This function
<a name="line187"></a> *     takes 3 arguments (the element, undefined, and the iterator) and should
<a name="line188"></a> *     return a boolean.  If the return value is true the element will be
<a name="line189"></a> *     included  in the returned iteror.  If it is false the element is not
<a name="line190"></a> *     included.
<a name="line191"></a> * @param {T=} opt_obj The object to be used as the value of &#39;this&#39; within
<a name="line192"></a> *     {@code f}.
<a name="line193"></a> * @return {!goog.iter.Iterator} A new iterator in which only elements that
<a name="line194"></a> *     passed the test are present.
<a name="line195"></a> * @template T
<a name="line196"></a> */
<a name="line197"></a>goog.iter.filter = function(iterable, f, opt_obj) {
<a name="line198"></a>  var iterator = goog.iter.toIterator(iterable);
<a name="line199"></a>  var newIter = new goog.iter.Iterator;
<a name="line200"></a>  newIter.next = function() {
<a name="line201"></a>    while (true) {
<a name="line202"></a>      var val = iterator.next();
<a name="line203"></a>      if (f.call(opt_obj, val, undefined, iterator)) {
<a name="line204"></a>        return val;
<a name="line205"></a>      }
<a name="line206"></a>    }
<a name="line207"></a>  };
<a name="line208"></a>  return newIter;
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Creates a new iterator that returns the values in a range.  This function
<a name="line214"></a> * can take 1, 2 or 3 arguments:
<a name="line215"></a> * &lt;pre&gt;
<a name="line216"></a> * range(5) same as range(0, 5, 1)
<a name="line217"></a> * range(2, 5) same as range(2, 5, 1)
<a name="line218"></a> * &lt;/pre&gt;
<a name="line219"></a> *
<a name="line220"></a> * @param {number} startOrStop  The stop value if only one argument is provided.
<a name="line221"></a> *     The start value if 2 or more arguments are provided.  If only one
<a name="line222"></a> *     argument is used the start value is 0.
<a name="line223"></a> * @param {number=} opt_stop  The stop value.  If left out then the first
<a name="line224"></a> *     argument is used as the stop value.
<a name="line225"></a> * @param {number=} opt_step  The number to increment with between each call to
<a name="line226"></a> *     next.  This can be negative.
<a name="line227"></a> * @return {!goog.iter.Iterator} A new iterator that returns the values in the
<a name="line228"></a> *     range.
<a name="line229"></a> */
<a name="line230"></a>goog.iter.range = function(startOrStop, opt_stop, opt_step) {
<a name="line231"></a>  var start = 0;
<a name="line232"></a>  var stop = startOrStop;
<a name="line233"></a>  var step = opt_step || 1;
<a name="line234"></a>  if (arguments.length &gt; 1) {
<a name="line235"></a>    start = startOrStop;
<a name="line236"></a>    stop = opt_stop;
<a name="line237"></a>  }
<a name="line238"></a>  if (step == 0) {
<a name="line239"></a>    throw Error(&#39;Range step argument must not be zero&#39;);
<a name="line240"></a>  }
<a name="line241"></a>
<a name="line242"></a>  var newIter = new goog.iter.Iterator;
<a name="line243"></a>  newIter.next = function() {
<a name="line244"></a>    if (step &gt; 0 &amp;&amp; start &gt;= stop || step &lt; 0 &amp;&amp; start &lt;= stop) {
<a name="line245"></a>      throw goog.iter.StopIteration;
<a name="line246"></a>    }
<a name="line247"></a>    var rv = start;
<a name="line248"></a>    start += step;
<a name="line249"></a>    return rv;
<a name="line250"></a>  };
<a name="line251"></a>  return newIter;
<a name="line252"></a>};
<a name="line253"></a>
<a name="line254"></a>
<a name="line255"></a>/**
<a name="line256"></a> * Joins the values in a iterator with a delimiter.
<a name="line257"></a> * @param {goog.iter.Iterable} iterable  The iterator to get the values from.
<a name="line258"></a> * @param {string} deliminator  The text to put between the values.
<a name="line259"></a> * @return {string} The joined value string.
<a name="line260"></a> */
<a name="line261"></a>goog.iter.join = function(iterable, deliminator) {
<a name="line262"></a>  return goog.iter.toArray(iterable).join(deliminator);
<a name="line263"></a>};
<a name="line264"></a>
<a name="line265"></a>
<a name="line266"></a>/**
<a name="line267"></a> * For every element in the iterator call a function and return a new iterator
<a name="line268"></a> * with that value.
<a name="line269"></a> *
<a name="line270"></a> * @param {goog.iter.Iterable} iterable The iterator to iterate over.
<a name="line271"></a> * @param {function(this:T,?,undefined,?):?} f The function to call for every
<a name="line272"></a> *     element.  This function
<a name="line273"></a> *     takes 3 arguments (the element, undefined, and the iterator) and should
<a name="line274"></a> *     return a new value.
<a name="line275"></a> * @param {T=} opt_obj The object to be used as the value of &#39;this&#39; within
<a name="line276"></a> *     {@code f}.
<a name="line277"></a> * @return {!goog.iter.Iterator} A new iterator that returns the results of
<a name="line278"></a> *     applying the function to each element in the original iterator.
<a name="line279"></a> * @template T
<a name="line280"></a> */
<a name="line281"></a>goog.iter.map = function(iterable, f, opt_obj) {
<a name="line282"></a>  var iterator = goog.iter.toIterator(iterable);
<a name="line283"></a>  var newIter = new goog.iter.Iterator;
<a name="line284"></a>  newIter.next = function() {
<a name="line285"></a>    while (true) {
<a name="line286"></a>      var val = iterator.next();
<a name="line287"></a>      return f.call(opt_obj, val, undefined, iterator);
<a name="line288"></a>    }
<a name="line289"></a>  };
<a name="line290"></a>  return newIter;
<a name="line291"></a>};
<a name="line292"></a>
<a name="line293"></a>
<a name="line294"></a>/**
<a name="line295"></a> * Passes every element of an iterator into a function and accumulates the
<a name="line296"></a> * result.
<a name="line297"></a> *
<a name="line298"></a> * @param {goog.iter.Iterable} iterable The iterator to iterate over.
<a name="line299"></a> * @param {function(this:T,V,?):V} f The function to call for every
<a name="line300"></a> *     element. This function takes 2 arguments (the function&#39;s previous result
<a name="line301"></a> *     or the initial value, and the value of the current element).
<a name="line302"></a> *     function(previousValue, currentElement) : newValue.
<a name="line303"></a> * @param {V} val The initial value to pass into the function on the first call.
<a name="line304"></a> * @param {T=} opt_obj  The object to be used as the value of &#39;this&#39;
<a name="line305"></a> *     within f.
<a name="line306"></a> * @return {V} Result of evaluating f repeatedly across the values of
<a name="line307"></a> *     the iterator.
<a name="line308"></a> * @template T,V
<a name="line309"></a> */
<a name="line310"></a>goog.iter.reduce = function(iterable, f, val, opt_obj) {
<a name="line311"></a>  var rval = val;
<a name="line312"></a>  goog.iter.forEach(iterable, function(val) {
<a name="line313"></a>    rval = f.call(opt_obj, rval, val);
<a name="line314"></a>  });
<a name="line315"></a>  return rval;
<a name="line316"></a>};
<a name="line317"></a>
<a name="line318"></a>
<a name="line319"></a>/**
<a name="line320"></a> * Goes through the values in the iterator. Calls f for each these and if any of
<a name="line321"></a> * them returns true, this returns true (without checking the rest). If all
<a name="line322"></a> * return false this will return false.
<a name="line323"></a> *
<a name="line324"></a> * @param {goog.iter.Iterable} iterable  The iterator object.
<a name="line325"></a> * @param {function(this:T,?,undefined,?):boolean} f  The function to call for
<a name="line326"></a> *     every value. This function
<a name="line327"></a> *     takes 3 arguments (the value, undefined, and the iterator) and should
<a name="line328"></a> *     return a boolean.
<a name="line329"></a> * @param {T=} opt_obj The object to be used as the value of &#39;this&#39; within
<a name="line330"></a> *     {@code f}.
<a name="line331"></a> * @return {boolean} true if any value passes the test.
<a name="line332"></a> * @template T
<a name="line333"></a> */
<a name="line334"></a>goog.iter.some = function(iterable, f, opt_obj) {
<a name="line335"></a>  iterable = goog.iter.toIterator(iterable);
<a name="line336"></a>  /** @preserveTry */
<a name="line337"></a>  try {
<a name="line338"></a>    while (true) {
<a name="line339"></a>      if (f.call(opt_obj, iterable.next(), undefined, iterable)) {
<a name="line340"></a>        return true;
<a name="line341"></a>      }
<a name="line342"></a>    }
<a name="line343"></a>  } catch (ex) {
<a name="line344"></a>    if (ex !== goog.iter.StopIteration) {
<a name="line345"></a>      throw ex;
<a name="line346"></a>    }
<a name="line347"></a>  }
<a name="line348"></a>  return false;
<a name="line349"></a>};
<a name="line350"></a>
<a name="line351"></a>
<a name="line352"></a>/**
<a name="line353"></a> * Goes through the values in the iterator. Calls f for each these and if any of
<a name="line354"></a> * them returns false this returns false (without checking the rest). If all
<a name="line355"></a> * return true this will return true.
<a name="line356"></a> *
<a name="line357"></a> * @param {goog.iter.Iterable} iterable  The iterator object.
<a name="line358"></a> * @param {function(this:T,?,undefined,?):boolean} f  The function to call for
<a name="line359"></a> *     every value. This function
<a name="line360"></a> *     takes 3 arguments (the value, undefined, and the iterator) and should
<a name="line361"></a> *     return a boolean.
<a name="line362"></a> * @param {T=} opt_obj The object to be used as the value of &#39;this&#39; within
<a name="line363"></a> *     {@code f}.
<a name="line364"></a> * @return {boolean} true if every value passes the test.
<a name="line365"></a> * @template T
<a name="line366"></a> */
<a name="line367"></a>goog.iter.every = function(iterable, f, opt_obj) {
<a name="line368"></a>  iterable = goog.iter.toIterator(iterable);
<a name="line369"></a>  /** @preserveTry */
<a name="line370"></a>  try {
<a name="line371"></a>    while (true) {
<a name="line372"></a>      if (!f.call(opt_obj, iterable.next(), undefined, iterable)) {
<a name="line373"></a>        return false;
<a name="line374"></a>      }
<a name="line375"></a>    }
<a name="line376"></a>  } catch (ex) {
<a name="line377"></a>    if (ex !== goog.iter.StopIteration) {
<a name="line378"></a>      throw ex;
<a name="line379"></a>    }
<a name="line380"></a>  }
<a name="line381"></a>  return true;
<a name="line382"></a>};
<a name="line383"></a>
<a name="line384"></a>
<a name="line385"></a>/**
<a name="line386"></a> * Takes zero or more iterators and returns one iterator that will iterate over
<a name="line387"></a> * them in the order chained.
<a name="line388"></a> * @param {...goog.iter.Iterator} var_args  Any number of iterator objects.
<a name="line389"></a> * @return {!goog.iter.Iterator} Returns a new iterator that will iterate over
<a name="line390"></a> *     all the given iterators&#39; contents.
<a name="line391"></a> */
<a name="line392"></a>goog.iter.chain = function(var_args) {
<a name="line393"></a>  var args = arguments;
<a name="line394"></a>  var length = args.length;
<a name="line395"></a>  var i = 0;
<a name="line396"></a>  var newIter = new goog.iter.Iterator;
<a name="line397"></a>
<a name="line398"></a>  /**
<a name="line399"></a>   * @return {*} The next item in the iteration.
<a name="line400"></a>   * @this {goog.iter.Iterator}
<a name="line401"></a>   */
<a name="line402"></a>  newIter.next = function() {
<a name="line403"></a>    /** @preserveTry */
<a name="line404"></a>    try {
<a name="line405"></a>      if (i &gt;= length) {
<a name="line406"></a>        throw goog.iter.StopIteration;
<a name="line407"></a>      }
<a name="line408"></a>      var current = goog.iter.toIterator(args[i]);
<a name="line409"></a>      return current.next();
<a name="line410"></a>    } catch (ex) {
<a name="line411"></a>      if (ex !== goog.iter.StopIteration || i &gt;= length) {
<a name="line412"></a>        throw ex;
<a name="line413"></a>      } else {
<a name="line414"></a>        // In case we got a StopIteration increment counter and try again.
<a name="line415"></a>        i++;
<a name="line416"></a>        return this.next();
<a name="line417"></a>      }
<a name="line418"></a>    }
<a name="line419"></a>  };
<a name="line420"></a>
<a name="line421"></a>  return newIter;
<a name="line422"></a>};
<a name="line423"></a>
<a name="line424"></a>
<a name="line425"></a>/**
<a name="line426"></a> * Builds a new iterator that iterates over the original, but skips elements as
<a name="line427"></a> * long as a supplied function returns true.
<a name="line428"></a> * @param {goog.iter.Iterable} iterable  The iterator object.
<a name="line429"></a> * @param {function(this:T,?,undefined,?):boolean} f  The function to call for
<a name="line430"></a> *     every value. This function
<a name="line431"></a> *     takes 3 arguments (the value, undefined, and the iterator) and should
<a name="line432"></a> *     return a boolean.
<a name="line433"></a> * @param {T=} opt_obj The object to be used as the value of &#39;this&#39; within
<a name="line434"></a> *     {@code f}.
<a name="line435"></a> * @return {!goog.iter.Iterator} A new iterator that drops elements from the
<a name="line436"></a> *     original iterator as long as {@code f} is true.
<a name="line437"></a> * @template T
<a name="line438"></a> */
<a name="line439"></a>goog.iter.dropWhile = function(iterable, f, opt_obj) {
<a name="line440"></a>  var iterator = goog.iter.toIterator(iterable);
<a name="line441"></a>  var newIter = new goog.iter.Iterator;
<a name="line442"></a>  var dropping = true;
<a name="line443"></a>  newIter.next = function() {
<a name="line444"></a>    while (true) {
<a name="line445"></a>      var val = iterator.next();
<a name="line446"></a>      if (dropping &amp;&amp; f.call(opt_obj, val, undefined, iterator)) {
<a name="line447"></a>        continue;
<a name="line448"></a>      } else {
<a name="line449"></a>        dropping = false;
<a name="line450"></a>      }
<a name="line451"></a>      return val;
<a name="line452"></a>    }
<a name="line453"></a>  };
<a name="line454"></a>  return newIter;
<a name="line455"></a>};
<a name="line456"></a>
<a name="line457"></a>
<a name="line458"></a>/**
<a name="line459"></a> * Builds a new iterator that iterates over the original, but only as long as a
<a name="line460"></a> * supplied function returns true.
<a name="line461"></a> * @param {goog.iter.Iterable} iterable  The iterator object.
<a name="line462"></a> * @param {function(this:T,?,undefined,?):boolean} f  The function to call for
<a name="line463"></a> *     every value. This function
<a name="line464"></a> *     takes 3 arguments (the value, undefined, and the iterator) and should
<a name="line465"></a> *     return a boolean.
<a name="line466"></a> * @param {T=} opt_obj This is used as the &#39;this&#39; object in f when called.
<a name="line467"></a> * @return {!goog.iter.Iterator} A new iterator that keeps elements in the
<a name="line468"></a> *     original iterator as long as the function is true.
<a name="line469"></a> * @template T
<a name="line470"></a> */
<a name="line471"></a>goog.iter.takeWhile = function(iterable, f, opt_obj) {
<a name="line472"></a>  var iterator = goog.iter.toIterator(iterable);
<a name="line473"></a>  var newIter = new goog.iter.Iterator;
<a name="line474"></a>  var taking = true;
<a name="line475"></a>  newIter.next = function() {
<a name="line476"></a>    while (true) {
<a name="line477"></a>      if (taking) {
<a name="line478"></a>        var val = iterator.next();
<a name="line479"></a>        if (f.call(opt_obj, val, undefined, iterator)) {
<a name="line480"></a>          return val;
<a name="line481"></a>        } else {
<a name="line482"></a>          taking = false;
<a name="line483"></a>        }
<a name="line484"></a>      } else {
<a name="line485"></a>        throw goog.iter.StopIteration;
<a name="line486"></a>      }
<a name="line487"></a>    }
<a name="line488"></a>  };
<a name="line489"></a>  return newIter;
<a name="line490"></a>};
<a name="line491"></a>
<a name="line492"></a>
<a name="line493"></a>/**
<a name="line494"></a> * Converts the iterator to an array
<a name="line495"></a> * @param {goog.iter.Iterable} iterable  The iterator to convert to an array.
<a name="line496"></a> * @return {!Array} An array of the elements the iterator iterates over.
<a name="line497"></a> */
<a name="line498"></a>goog.iter.toArray = function(iterable) {
<a name="line499"></a>  // Fast path for array-like.
<a name="line500"></a>  if (goog.isArrayLike(iterable)) {
<a name="line501"></a>    return goog.array.toArray(/** @type {!goog.array.ArrayLike} */(iterable));
<a name="line502"></a>  }
<a name="line503"></a>  iterable = goog.iter.toIterator(iterable);
<a name="line504"></a>  var array = [];
<a name="line505"></a>  goog.iter.forEach(iterable, function(val) {
<a name="line506"></a>    array.push(val);
<a name="line507"></a>  });
<a name="line508"></a>  return array;
<a name="line509"></a>};
<a name="line510"></a>
<a name="line511"></a>
<a name="line512"></a>/**
<a name="line513"></a> * Iterates over 2 iterators and returns true if they contain the same sequence
<a name="line514"></a> * of elements and have the same length.
<a name="line515"></a> * @param {goog.iter.Iterable} iterable1  The first iterable object.
<a name="line516"></a> * @param {goog.iter.Iterable} iterable2  The second iterable object.
<a name="line517"></a> * @return {boolean} true if the iterators contain the same sequence of
<a name="line518"></a> *     elements and have the same length.
<a name="line519"></a> */
<a name="line520"></a>goog.iter.equals = function(iterable1, iterable2) {
<a name="line521"></a>  iterable1 = goog.iter.toIterator(iterable1);
<a name="line522"></a>  iterable2 = goog.iter.toIterator(iterable2);
<a name="line523"></a>  var b1, b2;
<a name="line524"></a>  /** @preserveTry */
<a name="line525"></a>  try {
<a name="line526"></a>    while (true) {
<a name="line527"></a>      b1 = b2 = false;
<a name="line528"></a>      var val1 = iterable1.next();
<a name="line529"></a>      b1 = true;
<a name="line530"></a>      var val2 = iterable2.next();
<a name="line531"></a>      b2 = true;
<a name="line532"></a>      if (val1 != val2) {
<a name="line533"></a>        return false;
<a name="line534"></a>      }
<a name="line535"></a>    }
<a name="line536"></a>  } catch (ex) {
<a name="line537"></a>    if (ex !== goog.iter.StopIteration) {
<a name="line538"></a>      throw ex;
<a name="line539"></a>    } else {
<a name="line540"></a>      if (b1 &amp;&amp; !b2) {
<a name="line541"></a>        // iterable1 done but iterable2 is not done.
<a name="line542"></a>        return false;
<a name="line543"></a>      }
<a name="line544"></a>      if (!b2) {
<a name="line545"></a>        /** @preserveTry */
<a name="line546"></a>        try {
<a name="line547"></a>          // iterable2 not done?
<a name="line548"></a>          val2 = iterable2.next();
<a name="line549"></a>          // iterable2 not done but iterable1 is done
<a name="line550"></a>          return false;
<a name="line551"></a>        } catch (ex1) {
<a name="line552"></a>          if (ex1 !== goog.iter.StopIteration) {
<a name="line553"></a>            throw ex1;
<a name="line554"></a>          }
<a name="line555"></a>          // iterable2 done as well... They are equal
<a name="line556"></a>          return true;
<a name="line557"></a>        }
<a name="line558"></a>      }
<a name="line559"></a>    }
<a name="line560"></a>  }
<a name="line561"></a>  return false;
<a name="line562"></a>};
<a name="line563"></a>
<a name="line564"></a>
<a name="line565"></a>/**
<a name="line566"></a> * Advances the iterator to the next position, returning the given default value
<a name="line567"></a> * instead of throwing an exception if the iterator has no more entries.
<a name="line568"></a> * @param {goog.iter.Iterable} iterable The iterable object.
<a name="line569"></a> * @param {*} defaultValue The value to return if the iterator is empty.
<a name="line570"></a> * @return {*} The next item in the iteration, or defaultValue if the iterator
<a name="line571"></a> *     was empty.
<a name="line572"></a> */
<a name="line573"></a>goog.iter.nextOrValue = function(iterable, defaultValue) {
<a name="line574"></a>  try {
<a name="line575"></a>    return goog.iter.toIterator(iterable).next();
<a name="line576"></a>  } catch (e) {
<a name="line577"></a>    if (e != goog.iter.StopIteration) {
<a name="line578"></a>      throw e;
<a name="line579"></a>    }
<a name="line580"></a>    return defaultValue;
<a name="line581"></a>  }
<a name="line582"></a>};
<a name="line583"></a>
<a name="line584"></a>
<a name="line585"></a>/**
<a name="line586"></a> * Cartesian product of zero or more sets.  Gives an iterator that gives every
<a name="line587"></a> * combination of one element chosen from each set.  For example,
<a name="line588"></a> * ([1, 2], [3, 4]) gives ([1, 3], [1, 4], [2, 3], [2, 4]).
<a name="line589"></a> * @see http://docs.python.org/library/itertools.html#itertools.product
<a name="line590"></a> * @param {...!goog.array.ArrayLike.&lt;*&gt;} var_args Zero or more sets, as arrays.
<a name="line591"></a> * @return {!goog.iter.Iterator} An iterator that gives each n-tuple (as an
<a name="line592"></a> *     array).
<a name="line593"></a> */
<a name="line594"></a>goog.iter.product = function(var_args) {
<a name="line595"></a>  var someArrayEmpty = goog.array.some(arguments, function(arr) {
<a name="line596"></a>    return !arr.length;
<a name="line597"></a>  });
<a name="line598"></a>
<a name="line599"></a>  // An empty set in a cartesian product gives an empty set.
<a name="line600"></a>  if (someArrayEmpty || !arguments.length) {
<a name="line601"></a>    return new goog.iter.Iterator();
<a name="line602"></a>  }
<a name="line603"></a>
<a name="line604"></a>  var iter = new goog.iter.Iterator();
<a name="line605"></a>  var arrays = arguments;
<a name="line606"></a>
<a name="line607"></a>  // The first indicies are [0, 0, ...]
<a name="line608"></a>  var indicies = goog.array.repeat(0, arrays.length);
<a name="line609"></a>
<a name="line610"></a>  iter.next = function() {
<a name="line611"></a>
<a name="line612"></a>    if (indicies) {
<a name="line613"></a>      var retVal = goog.array.map(indicies, function(valueIndex, arrayIndex) {
<a name="line614"></a>        return arrays[arrayIndex][valueIndex];
<a name="line615"></a>      });
<a name="line616"></a>
<a name="line617"></a>      // Generate the next-largest indicies for the next call.
<a name="line618"></a>      // Increase the rightmost index. If it goes over, increase the next
<a name="line619"></a>      // rightmost (like carry-over addition).
<a name="line620"></a>      for (var i = indicies.length - 1; i &gt;= 0; i--) {
<a name="line621"></a>        // Assertion prevents compiler warning below.
<a name="line622"></a>        goog.asserts.assert(indicies);
<a name="line623"></a>        if (indicies[i] &lt; arrays[i].length - 1) {
<a name="line624"></a>          indicies[i]++;
<a name="line625"></a>          break;
<a name="line626"></a>        }
<a name="line627"></a>
<a name="line628"></a>        // We&#39;re at the last indicies (the last element of every array), so
<a name="line629"></a>        // the iteration is over on the next call.
<a name="line630"></a>        if (i == 0) {
<a name="line631"></a>          indicies = null;
<a name="line632"></a>          break;
<a name="line633"></a>        }
<a name="line634"></a>        // Reset the index in this column and loop back to increment the
<a name="line635"></a>        // next one.
<a name="line636"></a>        indicies[i] = 0;
<a name="line637"></a>      }
<a name="line638"></a>      return retVal;
<a name="line639"></a>    }
<a name="line640"></a>
<a name="line641"></a>    throw goog.iter.StopIteration;
<a name="line642"></a>  };
<a name="line643"></a>
<a name="line644"></a>  return iter;
<a name="line645"></a>};
<a name="line646"></a>
<a name="line647"></a>
<a name="line648"></a>/**
<a name="line649"></a> * Create an iterator to cycle over the iterable&#39;s elements indefinitely.
<a name="line650"></a> * For example, ([1, 2, 3]) would return : 1, 2, 3, 1, 2, 3, ...
<a name="line651"></a> * @see: http://docs.python.org/library/itertools.html#itertools.cycle.
<a name="line652"></a> * @param {!goog.iter.Iterable} iterable The iterable object.
<a name="line653"></a> * @return {!goog.iter.Iterator} An iterator that iterates indefinitely over
<a name="line654"></a> * the values in {@code iterable}.
<a name="line655"></a> */
<a name="line656"></a>goog.iter.cycle = function(iterable) {
<a name="line657"></a>
<a name="line658"></a>  var baseIterator = goog.iter.toIterator(iterable);
<a name="line659"></a>
<a name="line660"></a>  // We maintain a cache to store the iterable elements as we iterate
<a name="line661"></a>  // over them. The cache is used to return elements once we have
<a name="line662"></a>  // iterated over the iterable once.
<a name="line663"></a>  var cache = [];
<a name="line664"></a>  var cacheIndex = 0;
<a name="line665"></a>
<a name="line666"></a>  var iter = new goog.iter.Iterator();
<a name="line667"></a>
<a name="line668"></a>  // This flag is set after the iterable is iterated over once
<a name="line669"></a>  var useCache = false;
<a name="line670"></a>
<a name="line671"></a>  iter.next = function() {
<a name="line672"></a>    var returnElement = null;
<a name="line673"></a>
<a name="line674"></a>    // Pull elements off the original iterator if not using cache
<a name="line675"></a>    if (!useCache) {
<a name="line676"></a>      try {
<a name="line677"></a>        // Return the element from the iterable
<a name="line678"></a>        returnElement = baseIterator.next();
<a name="line679"></a>        cache.push(returnElement);
<a name="line680"></a>        return returnElement;
<a name="line681"></a>      } catch (e) {
<a name="line682"></a>        // If an exception other than StopIteration is thrown
<a name="line683"></a>        // or if there are no elements to iterate over (the iterable was empty)
<a name="line684"></a>        // throw an exception
<a name="line685"></a>        if (e != goog.iter.StopIteration || goog.array.isEmpty(cache)) {
<a name="line686"></a>          throw e;
<a name="line687"></a>        }
<a name="line688"></a>        // set useCache to true after we know that a &#39;StopIteration&#39; exception
<a name="line689"></a>        // was thrown and the cache is not empty (to handle the &#39;empty iterable&#39;
<a name="line690"></a>        // use case)
<a name="line691"></a>        useCache = true;
<a name="line692"></a>      }
<a name="line693"></a>    }
<a name="line694"></a>
<a name="line695"></a>    returnElement = cache[cacheIndex];
<a name="line696"></a>    cacheIndex = (cacheIndex + 1) % cache.length;
<a name="line697"></a>
<a name="line698"></a>    return returnElement;
<a name="line699"></a>  };
<a name="line700"></a>
<a name="line701"></a>  return iter;
<a name="line702"></a>};
</pre>


</body>
</html>
